*********************************************************************************
** Education Service Provision Audit Analysis
*********************************************************************************

	capture log close
	clear all
	macro drop _all
	set more off
	version 14.2
	set matsize 11000

********************************************************************************
 *Set working directory 

capture cd "replication_APSR"
	
********************************************************************************

use "School_audits.dta"

******************************************************

*Just use the questions which pertain to public service delivery standards, which are:
*One teacher should teach a maximum of 45 children in one classroom
*One textbook should be shared by no more than 3 children
*One desk should be shared by no more than 3 children 
*One latrine stance should be used by no more than 40 pupils

*I suggest we use teacher student ration (and change in) and student latrine ratio to test for changes with regards to public service standards.
*To test for differences in the performance of the lc5 councilors we could use:
	*government teachers hired in past 3 months - public service improvement
	*number of applications to the DEO's office - change in attitude of schools in treatment areas
	*number of visits from regular and special woman councilor - change in performance of councilors

********************************************************************************
********************************************************
* Define covariates
******************************************************
encode dist_name, gen(distid)

* define competitive moderator equal 1 if at least one councilors is in a competitive constituency 
gen competitive=0
replace competitive=1 if rc_competitive==1 | swc_competitive==1
tab competitive

gen pop1000 = subc_totalpop/1000
lab var pop1000 "Subcounty population (thousands)"

* define covariates
gl covs rc_NRM swc_NRM rc_Edu swc_Edu rc_FirstTerm swc_FirstTerm rc_educ swc_educ rc_cterms swc_cterms rc_polexpterms swc_polexpterms rc_counterms swc_counterms pop1000 subc_hhi 

	* assign mean values of control to missing values of covariates (Lin, green and Coppock, 2015))
	foreach y in $covs {
	 egen `y'_mean=mean(`y')
	 gen `y'_miss = `y'==.
	 replace `y' = `y'_mean if `y'==.
	 }

gl controls rc_NRM swc_NRM rc_Edu swc_Edu  rc_FirstTerm swc_FirstTerm pop1000 pop1000_miss subc_hhi rc_NRM_miss swc_NRM_miss rc_FirstTerm_miss swc_FirstTerm_miss subc_hhi_miss	 
su $controls

encode dist_name, gen(distrid)

* define sample
gen sample =0
replace sample=1 if rc_NRM!=. & swc_NRM!=. & rc_Edu!=. & swc_Edu!=. & c.pop1000!=. & c.subc_hhi!=. & c.subc_poverty!=.


* DV
**************
set more off

gl DVs Absenteeism Teachers_Pupils Teachers_Added Teachers_Apps StaffMeeting
su $DVs


********************************************************
* unconditional treatment effects
********************************************************
set more off

forvalues i=0(1)1{
foreach y in $DVs{
	
reghdfe `y' i.ID if sample==1 & phase==`i', abs(distid) vce(r)
	estadd local control ""
	estadd local district "X"
	estimate store `y'_p`i'_m1

reghdfe `y' i.ID $controls if sample==1 & phase==`i', abs(distid) vce(r)
	estadd local control "X"
	estadd local district "X"
	estimate store `y'_p`i'_m2 	
	margins , dydx(ID) post

********************************************************
* effects conditional on competitiveness
******************************************************
* Binary

reghdfe `y' ID##competitive if sample==1 & phase==`i', abs(distid) vce(r)
	estadd local control ""
	estadd local district "X"
	estimate store `y'_p`i'_m3
	
reghdfe `y' ID##competitive $controls if sample==1 & phase==`i', abs(distid) vce(r)
	estadd local control "X"
	estadd local district "X"
	estimate store `y'_p`i'_m4
	margins, dydx(ID) over(competitive) post
	estimate restore `y'_p`i'_m4
	margins r.ID, over(r.competitive) post

	}
	}
	
*Table  online appendix;

*Table 27 midline panel

# delimit ; 
esttab  Absenteeism_p0_m1 Absenteeism_p0_m2  
		Teachers_Pupils_p0_m1 Teachers_Pupils_p0_m2 
		Teachers_Added_p0_m1 Teachers_Added_p0_m2 
		Teachers_Apps_p0_m1 Teachers_Apps_p0_m2
		StaffMeeting_p0_m1 StaffMeeting_p0_m2 
using "tables/SchoolUnconditional_phase0.tex",
		keep(1.ID  )
	    order(1.ID )
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01) 					
		mgroups("\textbf{Absenteeism}" "\textbf{Teacher-puppils ratio}"
		"\textbf{STeachers hired}" "\textbf{Teacher applications}" "\textbf{Staff meetings}", pattern(1 0 1 0 1 0 1 0 1 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		varlabels(1.ID "ID")
		stats(district control N, labels("District FE" "Controls" "N")
		fmt(0 0)) collabels(none) label booktabs nonotes
		replace;
		#delimit cr

*Table 27 endline panel

# delimit ; 
esttab  Absenteeism_p1_m1 Absenteeism_p1_m2  
		Teachers_Pupils_p1_m1 Teachers_Pupils_p1_m2 
		Teachers_Added_p1_m1 Teachers_Added_p1_m2 
		Teachers_Apps_p1_m1 Teachers_Apps_p1_m2
		StaffMeeting_p1_m1 StaffMeeting_p1_m2 
using "tables/SchoolUnconditional_phase1.tex",
		keep(1.ID  )
	    order(1.ID )
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01) 					
		mgroups("\textbf{Absenteeism}" "\textbf{Teacher-puppils ratio}"
		"\textbf{STeachers hired}" "\textbf{Teacher applications}" "\textbf{Staff meetings}", pattern(1 0 1 0 1 0 1 0 1 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		varlabels(1.ID "ID")
		stats(district control N, labels("District FE" "Controls" "N")
		fmt(0 0)) collabels(none) label booktabs nonotes
		replace;
		#delimit cr		

* Conditional effects
*Table 29 midline panel
		
		# delimit ; 
esttab  Absenteeism_p0_m3 Absenteeism_p0_m4  
		Teachers_Pupils_p0_m3 Teachers_Pupils_p0_m4 
		Teachers_Added_p0_m3 Teachers_Added_p0_m4 
		Teachers_Apps_p0_m3 Teachers_Apps_p0_m4
		StaffMeeting_p0_m3 StaffMeeting_p0_m4 
using "tables/SchoolConditional_phase0.tex",
		keep(1.ID 1.competitive 1.ID#1.competitive )
	    order(1.ID 1.competitive 1.ID#1.competitive )
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01) 					
		mgroups("\textbf{Absenteeism}" "\textbf{DHO applications}"
		"\textbf{New Employees}" "\textbf{Outreach campaigns}" "\textbf{Staff meetings}", pattern(1 0 1 0 1 0 1 0 1 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		varlabels(1.ID "ID" 1.competitive "Competitive"
				  1.ID#1.phase "\$ID\times Competitive$")
		stats(district control N, labels("District FE" "Controls" "N")
		fmt(0 0)) collabels(none) label booktabs nonotes
		replace;
		#delimit cr

*Table 29 endline panel
		
# delimit ; 
esttab  Absenteeism_p1_m3 Absenteeism_p1_m4  
		Teachers_Pupils_p1_m3 Teachers_Pupils_p1_m4 
		Teachers_Added_p1_m3 Teachers_Added_p1_m4 
		Teachers_Apps_p1_m3 Teachers_Apps_p1_m4
		StaffMeeting_p1_m3 StaffMeeting_p1_m4 
using "tables/SchoolConditional_phase1.tex",
		keep(1.ID 1.competitive 1.ID#1.competitive )
	    order(1.ID 1.competitive 1.ID#1.competitive )
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01) 					
		mgroups("\textbf{Absenteeism}" "\textbf{DHO applications}"
		"\textbf{New Employees}" "\textbf{Outreach campaigns}" "\textbf{Staff meetings}", pattern(1 0 1 0 1 0 1 0 1 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		varlabels(1.ID "ID" 1.competitive "Competitive"
				  1.ID#1.phase "\$ID\times Competitive$")
		stats(district control N, labels("District FE" "Controls" "N")
		fmt(0 0)) collabels(none) label booktabs nonotes
		replace;
		#delimit cr
